<?PHP
	require 'includes/master.inc.php';
	$auth->admin();

	$app = new App($_GET['app_id']);
	if($app->id == "") redirect("index.php");

	if(preg_match('/[0-9.]+/', $app->currentVersion()) == 1)
	{
		$version = explode(".", $app->currentVersion());
		$version[count($version) - 1]++;
		$version = implode(".", $version);
	}
	else
		$version = "";
	
	$title   = $app->name . " " . $version;
	$changes = "";
	
	if(isset($_POST['btnsubmit']))
	{
		$Error->blank($_POST['version'], "version");
		$Error->blank($_POST['title'], "title");
		$Error->upload($_FILES['file'], "file");

		if($Error->ok())
		{
			$v = new Version();
			$v->title   = $_POST['title'];
			$v->version = $_POST['version'];
			$v->changes = "<ul>\n<li>" . implode("</li>\n<li>", explode("\r\n", $_POST['changes'])) . "</li>\n</ul>";
			$v->app_id  = $app->id;
			$v->dt      = dater();
			$v->length  = filesize($_FILES['file']['tmp_name']);
			$v->md5     = md5_file($_FILES['file']['tmp_name']);
			$v->ext     = substr($_FILES['file']['name'], -3);
			$v->insert();

			if($app->upload_type == "s3")
			{
				$s3 = new S3($app->s3key, $app->s3pkey);

				$object = strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $app->name)) . "_" . $v->version . "." . $v->ext;
				$info   = parse_url($app->s3path);
				$object = slash($info['path']) . $object;
				chmod($_FILES['file']['tmp_name'], 0755);
				$s3->putObject($app->s3bucket, $object, $_FILES['file']['tmp_name'], true);
			}
			else
			{
				$fn = "uploads/" . strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $app->name)) . "_" . $v->version . "." . $v->ext;
				move_uploaded_file($_FILES['file']['tmp_name'], $fn);
			}

			redirect("versions.php?app_id=" . $app->id);
		}
		else
		{
			$version = htmlspecialchars($_POST['version']);
			$title   = htmlspecialchars($_POST['title']);
			$changes = htmlspecialchars($_POST['changes']);
		}
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title><?PHP echo $app->__name;?> New Version | Appcaster</title>
	<?PHP include 'inc/meta.inc.php'; ?>
	<?PHP $Error->css(); ?>
	<script type="text/javascript" charset="utf-8">
		$(function() {
			$('#version').focus().keyup(function() {
				$('#title').val('<?PHP echo $app->name;?> ' + $('#version').val());
			});
		});
	</script>
</head>

<body id="home">
	<?PHP include 'inc/head.inc.php'; ?>
	<div id="nav">
		<ul>
			<li class='selected'><a href='<?PHP WEBROOT();?>index.php'>Applications</a></li>
			<li><a href='<?PHP WEBROOT();?>orders.php'>Orders</a></li>
			<li><a href='<?PHP WEBROOT();?>feedback.php'>Feedback (<?PHP echo Feedback::newCountTotal();?>)</a></li>
			<li><a href='<?PHP WEBROOT();?>stats.php'>Stats</a></li>
			<li><a href='<?PHP WEBROOT();?>options.php'>Options</a></li>
		</ul>
	</div>

	<div id="menutabs">
		<ul id="tabnav">
			<li><a href='<?PHP WEBROOT();?>index.php'>Application List</a></li>
			<li><a href='<?PHP WEBROOT();?>newapp.php'>New App</a></li>
			<li><a href='<?PHP WEBROOT();?>editapp.php?app_id=<?PHP echo $app->id;?>'><?PHP echo $app->__name;?></a></li>
			<li><a href='<?PHP WEBROOT();?>orders.php?app_id=<?PHP echo $app->id;?>'>Orders</a></li>
			<li><a href='<?PHP WEBROOT();?>versions.php?app_id=<?PHP echo $app->id;?>'>Versions</a></li>
			<li class="selected"><a href='<?PHP WEBROOT();?>newversion.php?app_id=<?PHP echo $app->id;?>'>New Version</a></li>
		</ul>
	</div>

	<div id="contain">
		<?PHP echo $Error; ?>
		<form action="" method="post" enctype="multipart/form-data">
			<fieldset>
				<legend>Version Info</legend>
				<p><label for="version">Version:</label> <input type="text" name="version" value="<?PHP echo $version;?>" class="text" id="version"/></p>
				<p><label for="title">Title:</label> <input type="text" name="title" value="<?PHP echo $title;?>" class="text" id="title"/></p>
				<p><label for="file">File:</label> <input type="file" name="file" value="" id="file"/> Max Upload <?PHP echo ini_get('upload_max_filesize');?></p>
				<p><label for="changes">Release Notes:</label> (Each line will be wrapped in a &lt;li&gt; tag to create a bulleted list.)<br/><textarea name="changes" id="changes" class="medium"><?PHP echo $changes; ?></textarea></p>
				<p><input type="submit" name="btnsubmit" value="Upload New Version" id="btnsubmit"/></p>
			</fieldset>
		</form>
	</div>
</body>
</html>